package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;


@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    @Override
    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        // shoppingCartDTO  菜品id+口味  或者 是  套餐id
        // 1: 新建购物车对象
        ShoppingCart shoppingCart = new ShoppingCart();
        // 2: 所需要添加的所有数据设置到 shoppingCart 中
        // 2.1 这是谁的购物项呢 ？ -- 谁登录是谁的
        // 获取用户id   登录的时候把用户id存到令牌中 拦截器解析token取出来了
        // 放到 BaseContext中 可以从 BaseContext取出来
        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);//把那菜品id+口味  或者 是  套餐id存到shoppingcart中
        //2.2 判断一下 新加的购物项是否在 数据库表中存在
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.list(shoppingCart);// dish_id  dish_flavor  setmeal_id     user_id
        // 如果存在 要更新的是份数
        if(shoppingCartList!=null && shoppingCartList.size()==1){//查到了 存在
            // 更新份数
            shoppingCart = shoppingCartList.get(0);//从数据库查出已经有的 购物项信息 赋值给 shoppingCart
            //更新份数
            shoppingCart.setNumber(shoppingCart.getNumber()+1);
            // 执行更新语句
            shoppingCartMapper.update(shoppingCart);
        }else {
            //  如果没有存在就新加购物项
            //你怎么知道是菜品还是套餐
            Long dishId = shoppingCart.getDishId();
            if(dishId!=null){//菜品
                // 根据菜品id 查询菜品信息
                Dish dish = dishMapper.getById(dishId);
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());
            }else{//套餐
                // 查询套餐信息
                Setmeal setmeal = setmealMapper.getById(shoppingCart.getSetmealId());
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setAmount(setmeal.getPrice());
            }
            //份数  创建时间
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());

            // 新增
            shoppingCartMapper.insert(shoppingCart);
        }






    }

    @Override
    public List<ShoppingCart> showShoppingCart() {
            // 查询该用户的购物车数据
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setUserId(BaseContext.getCurrentId());
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        return list;
    }

    @Override
    public void clean() {
        shoppingCartMapper.delete(BaseContext.getCurrentId());//根据用户id删除
    }

    @Override
    public void delete(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        //new了一个对象
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
//设置查询条件，查询当前登录用户的购物车数据
        shoppingCart.setUserId(BaseContext.getCurrentId());

        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);

        if (list != null&&list.size()>0){
            shoppingCart = list.get(0);

            Integer number = shoppingCart.getNumber();
            if (number == 1){
                //当前商品在购物车中的份数为1，直接删除当前记录
                shoppingCartMapper.deleteById(shoppingCart.getId());
            }else {
                //当前商品在购物车中的份数不为1，修改份数即可
                shoppingCart.setNumber(shoppingCart.getNumber()-1);

                shoppingCartMapper.update(shoppingCart);
            }
        }

    }
}
